﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Interface;
using System.Configuration;
using Phoenix.ReflectionExtensions;
using PServiceBus.Core.Runtime;
using PServiceBus.Core.Logger;
using PServiceBus.Core.Configuration;
using PServiceBus.Core.Runtime.Configuration;

namespace PServiceBus.Core.Manager {
    public class WebStreamManagementManager {
        static List<IWebStreamManagement> _streams;
        public static List<IWebStreamManagement> Streams {
            get {
                if (_streams == null) {
                    _streams = new List<IWebStreamManagement>();
                    foreach (var stream in ConfigurationProvider.WebStreamManagementConfigs) {
                        var ex = MethodHelper.Try(() =>
                        {
                            var messageStore = DynamicBroker.Broker[stream.Type].New() as IWebStreamManagement;
                            _streams.Add(messageStore);
                        });
                        ESBLogger.Log(ex);
                    }
                }
                return _streams;
            }
        }

        public static void Execute(Action<IWebStreamManagement> action) {
            if (action == null) return;
            foreach (var stream in Streams) {
                MethodHelper.Try(() => action(stream));
            }
        }
    }
}
